$(function(){
	page.bind();
	page.getDeptName('1');
	page.getDeptManagerList();
	queryDept('1');
	page.getAdminDept();
	page.removeBtnBind();
})
var page = {
	bind:function(){
		//点击导入部门按钮
		$("#import").bind("click",function(){
			$("#importDeptModal").modal("show");
		});
		//点击导入部门模态框的确定按钮
		$("#addModalBtn").bind("click",function(){
			var deptStr = "";
			$(".right-box li.item").each(function(){
				deptStr+=$(this).attr("value")+",";
			});	
			deptStr = deptStr.substring(0,deptStr.length-1);
			console.log(deptStr);
			if(deptStr.length<=0){
				layAlert("没有选择要导入的新部门");
				return false;
			}
			ajaxUtil.ajaxRuquest("basicData/importDept",{'deptStr':deptStr},function(data){
				layAlert(data);
			});
			page.getDeptName('1');
			page.getAdminDept();
			dataTable.ajax.reload(null,false);
			$("#importDeptModal").modal("hide");
		});
		//点击切换查看状态按钮
		$("#view").bind("click",function(){
			var view = $("#view").val();
			if(view=="1"){
				view = "0";
			}else{
				view = "1";
			}
			page.getDeptName(view);
			queryDept(view);
			$("#view").val(view);
		});
		$('#addDeptModal').on('show.bs.modal', function () {
			$("#add").unbind("click");
			$("#add").bind("click",function(){
				var deptName = $("#modalDeptName").val();
				if(deptName==""){
					layAlert("请填写部门名称");
					return false;
				}
				var status = $("#modalStatus").val();
				var deptManager = $("#modalDeptManager").val();
				if(deptManager=="请选择"){
					deptManager = "";
				}
				var isProductionDept = $("#modalIsProductionDept").val();
				var dailyCapacity = 0;
				if(isProductionDept==1){
					dailyCapacity = $("#modalDailyCapacity").val();
					if(/^\d+$/.test(dailyCapacity)==false){
						layer.msg("日产能只能是正整数",{'icon':0});
						return false;
					}
				}
				var productionCycle = $("#modalProductionCycle").val();
				if(productionCycle!="" && /^\d+$/.test(productionCycle)==false){
					layer.msg("部门生产周期只能是正整数",{'icon':0});
					return false;
				}
				var dailyWorkingHours = $("#modalDailyWorkingHours").val();
				if(dailyWorkingHours!="" && (/^[0-9]+([.][0-9]+){0,1}$/.test(dailyWorkingHours)==false 
						|| dailyWorkingHours<0 || dailyWorkingHours>24)){
					layer.msg("日工作时长（小时） 格式不正确，请输入0-24之间的数值",{'icon':0});
					return false;
				}
				var isProductionDept = $("#modalIsProductionDept").val();
				var restType = $("#modalRestType").val();
				var deptPhone = $("#modalDeptPhone").val();
				//insert
				ajaxUtil.ajaxRuquest(
						"basicData/insertDept",
						{
							'deptName':deptName,
							'status':status,
							'deptManager':deptManager,
							'productionCycle':productionCycle,
							'isProductionDept':isProductionDept,
							'dailyCapacity':dailyCapacity,
							'restType':restType,
							'deptPhone':deptPhone,
							'dailyWorkingHours':dailyWorkingHours
						},
						function(data){
							//点击提交按钮后刷新表
							page.getAdminDept();
							dataTable.ajax.reload(null,false);
							page.getDeptName($("#view").val());
							$("#addDeptModal").modal("hide");
						}
				);
			});
		});
		//改变部门名称时
		$("#deptName").bind("change",function(){
			var deptName = $("#deptName option:selected").text();
			page.getDeptDetail(deptName);
		});
		//点击+部门按钮时
		$("#addDeptBtn").bind("click",function(){
			$("#addDeptNameTr").show("slow");
		});
		//点击添加按钮时
		$("#addDeptNameBtn").bind("click",function(){
			var addDeptName = $("#addDeptName").val();
			if(addDeptName==""){
				layAlert("请输入部门名称");
				return false;
			}
			$("#addDeptNameTr").hide();
			$("#deptName").append("<option value='"+addDeptName+"'>"+addDeptName+"</option>");
			$("#deptName option[value='"+addDeptName+"']").attr("selected","selected");
			page.getDeptDetail(addDeptName);
		});
		//点击修改按钮时
		$("#reportBtn").bind("click",function(){
			var deptName = $("#deptName option:selected").val();
			var status = $("#status").val();
			if(deptName=="请选择"){
				layAlert("请选择部门");
				return false;
			}
			if(status=="请选择"){
				layAlert("请选择状态");
				return false;
			}
			var productionCycle = $("#productionCycle").val();
			if(productionCycle!="" && /^\d+$/.test(productionCycle)==false){
				layer.msg("部门生产周期只能是正整数",{'icon':0});
				return false;
			}
			var isProductionDept = $("#isProductionDept").val();
			var dailyCapacity = 0;
			if(isProductionDept==1){
				dailyCapacity = $("#dailyCapacity").val();
				if(/^\d+$/.test(dailyCapacity)==false){
					layer.msg("日产能只能是正整数",{'icon':0});
					return false;
				}
			}
			var dailyWorkingHours = $("#dailyWorkingHours").val();
			if(dailyWorkingHours=="" && (/^[0-9]+([.][0-9]+){0,1}$/.test(dailyWorkingHours)==false 
					|| dailyWorkingHours<0 || dailyWorkingHours>24)){
				layer.msg("日工作时长（小时） 格式不正确，请输入0-24之间的数值",{'icon':0});
				return false;
			}
			ajaxUtil.ajaxRuquest(
					"basicData/updateDept",
					page.updateParam(),
					function(data){
						//点击提交按钮后刷新表
						page.getAdminDept();
						page.getDeptName($("#view").val());
						dataTable.ajax.reload(null,false);
						layAlert("修改成功");
					}
			);
		});
	},
	//获取admin公司的部门信息
	getAdminDept:function(){
		ajaxUtil.ajaxRuquest("basicData/getAdminDept",{},function(data){
			console.log(data);
			$(".left-box").html("");
			for(var i=0,l=data.length;i<l;i++){
				$(".left-box").append("<li class='item ui-sortable-handle' value='"+data[i].id+"' >"+data[i].deptName+"</li>");
			}
			page.leftAndRightBind();
		});
	},
	//左右移动
	leftAndRightBind:function(){
		$(".item").unbind("click");
		$(".left-box .item").unbind("dblclick");
		$(".right-box .item").unbind("dblclick");
		//单击
		$(".item").bind("click",function(){
			$(this).toggleClass("selected-item");
		});
		//左边双击右移
		$(".left-box .item").bind("dblclick",function(){
			$(".right-box").append($(this).removeClass('selected-item'));
			page.leftAndRightBind();
		});
		//右边双击左移
		$(".right-box .item").bind("dblclick",function(){
			$(".left-box").append($(this).removeClass('selected-item'));
			page.leftAndRightBind();
		});
	},
	removeBtnBind:function(){
		$(".add-one").bind("click",function(){
			$(".right-box").append($(".left-box").find('.selected-item').removeClass('selected-item'));
		});
		$(".add-all").bind("click",function(){
			$(".right-box").append($(".left-box").find('.item').removeClass('selected-item'));
		});
		$(".remove-one").bind("click",function(){
			$(".left-box").append($(".right-box").find('.selected-item').removeClass('selected-item'));
		});
		$(".remove-all").bind("click",function(){
			$(".left-box").append($(".right-box").find('.item').removeClass('selected-item'));
		});
	},
	//获取部门名称
	getDeptName:function(view){
		ajaxUtil.ajaxRuquest('basicData/getDeptName',
				{'view':view},
				function(data){
			var html = "<option>请选择</option>";
			console.log(view);
			if(view=="1"){
				$(".right-box").html("");
				for(var j=0,l=data.length;j<l;j++){
					$(".right-box").append("<li class='ui-sortable-handle' value='"+data[j].id+"' >"+data[j].deptName+"</li>");
				}
			}
			for(var i=0;i<data.length;i++){
				html = html+"<option value='"+data[i].id+"'>"+data[i].deptName+"</option>"
			}
			$("#deptName").html("");
			$("#deptName").append(html);
		});
	},
	//获取部门经理列表
	getDeptManagerList:function(){
		ajaxUtil.ajaxRuquest('basicData/getDeptManagerList',null,function(data){
			var html = "<option>请选择</option>";
			for(var i=0;i<data.length;i++){
				html = html+"<option value='"+data[i].id+"'>"+data[i].personName+"</option>"
			}
			$("#deptManager").append(html);
			$("#modalDeptManager").append(html);
		});
	},
	//获取部门详情
	getDeptDetail:function(deptName){
		console.log(deptName);
		ajaxUtil.ajaxRuquest(
				'basicData/getDeptDetail',
				{
					'deptName':deptName,
					'view':$("#view").val()
				},
				function(data){
					console.log(data);
					if(data!=""){
						$("#departmentCode").val(data[0].id);
						$("#deptName").val(data[0].id);
						$("#status").val(data[0].status);
						$("#deptManager").val(data[0].deptManager);
						$("#restType").val(data[0].restType);
						$("#deptPhone").val(data[0].deptPhone);
						$("#auditor").val(data[0].auditor);
						$("#creater").val(data[0].creater);
						$("#creationTime").val(data[0].creationTime);
						$("#lastModifier").val(data[0].lastModifier);
						$("#lastModifyTime").val(data[0].lastModifyTime);
						$("#sort").val(data[0].sort);
						$("#reportBtn").val(data[0].id);
						$("#productionCycle").val(data[0].productionCycle);
						$("#isProductionDept").val(data[0].isProductionDept);
						$("#dailyCapacity").val(data[0].dailyCapacity);
						$("#dailyWorkingHours").val(data[0].dailyWorkingHours);
					}
				}
		)
	},
	//update的参数
	updateParam:function(){
		var departmentCode = $("#departmentCode").val();
		var deptName = $("#deptName option:selected").text();
		var status = $("#status option:selected").val();
		var deptManager = $("#deptManager").val();
		if(deptManager=="请选择"){
			deptManager = "";
		}
		var productionCycle = $("#productionCycle").val();
		var isProductionDept = $("#isProductionDept").val();
		var dailyCapacity = 0;
		if(isProductionDept==1){
			dailyCapacity = $("#dailyCapacity").val();
			if(/^\d+$/.test(dailyCapacity)==false){
				layer.msg("日产能只能是正整数");
				return false;
			}
		}
		var dailyWorkingHours = $("#dailyWorkingHours").val();
		var restType = $("#restType").val();
		var deptPhone = $("#deptPhone").val();
		var auditor = $("#auditor").val();
		var sort = $("#sort").val();
		var updateId = $("#reportBtn").val();
		console.log(updateId);
		var sendData = {
				'departmentCode':departmentCode,
				'deptName':deptName,
				'status':status,
				'deptManager':deptManager,
				'productionCycle':productionCycle,
				'isProductionDept':isProductionDept,
				'dailyCapacity':dailyCapacity,
				'dailyWorkingHours':dailyWorkingHours,
				'restType':restType,
				'deptPhone':deptPhone,
				'auditor':auditor,
				'sort':sort,
				'id':updateId
		}
		return sendData;
	}
}
var tableData=[];//查询部门结果
//查询所有部门
var dataTable;
function queryDept(view){
	if(view==undefined){
		view = '1';
	}
	$('#queryDeptTable tbody').unbind("click");
	dataTable = ajaxUtil.dataTablesAjax("queryDeptTable",10,"basicData/queryDept",
			{'view':view},[
				{"data":"departmentCode"},
				{"data":"deptName"},
				{"data":"status"}
			]);
	//行点击事件
	$('#queryDeptTable tbody').on('click', 'tr', function () {
        var data = dataTable.row(this).data();
        var departmentCode = data.departmentCode;
        page.getDeptDetail(departmentCode);
    });
}
